<?php
namespace data\service;

use data\model\Article as articleModel;
use think\facade\Session;
class ArticleService extends BaseService{

    // 模型初始化
    public function __construct()
    {
        $this->model = new articleModel;
    }

    /**
     * 获取文章类型
     */
    public function getArticleType(){
        return ArticleModel::getArticleType();
    }

    /**
     * 编辑操作
     */
    public function editOption($id,$option){
        $art = $this->model->find($id);
        $options = json_decode($art->option,true);
        if($key = in_array($option,$options)){
            unset($options[$key]);
        }else{
            array_push($options,$option);
        }
        $art->option = json_encode(array_values($options));
        $art->save();

        return true;
    }

    /**
     * 获取全部类型推荐文章
     * @desc 并且技术文在最前面
     * @author 16
     * @date 2018/2/23
     */
    public  function getRecommendAericle(){
        return $this->model->where('option','like','%'.'recommend'.'%')->select();
    }

    /**
     * 获取置顶文章
     * @desc
     * @author 16
     * @date 2018/2/23
     */
    public  function getHotAericle($type=null){
        $type && $where[] = ['type_id','=',$type];
        $where[] = ['option','like','%'.'top'.'%'];
        return $this->model->where($where)->field('id,title')->select();
    }

    /**
     * 获取相应的文章
     * @desc 根据条件查询文章
     * @param $map
     * @param $field
     * @author 16
     * @date 2018/2/24
     */
    public static function getArticleByMap($map,$field='*'){
        $map[] = ['option','like','%show%'];
        return articleModel::where($map)->order('create_time DESC')->field($field)->select();
    }

    /**
     * 获取前 n 条数据
     * @desc
     * @param $limit
     * @param string $field
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @return array|\PDOStatement|string|\think\Collection
     * @author 16
     * @date 2018/2/25
     */
    public static function getArticleLimit($limit, $field='*'){
        return articleModel::where('option','like','%'.'show'.'%')->order('create_time DESC')->limit($limit)->field($field)->select();
    }

}